import axios from 'axios';

const VOLCANO_ARK_API_KEY = import.meta.env.VITE_VOLCANO_ARK_API_KEY;
const VOLCANO_ARK_MODEL_NAME = 'doubao-seed-1-6-251015'; // 您指定的模型名称

if (!VOLCANO_ARK_API_KEY) {
  console.error('Volcano Ark API Key is not set in environment variables.');
}

export async function sendMessageToVolcanoArk(
  userId: string,
  message: string,
  conversationId?: string
): Promise<string> {
  if (!VOLCANO_ARK_API_KEY) {
    return 'Error: Volcano Ark API Key is not configured.';
  }

  try {
    const response = await axios.post(
      `/volcanoark/api/v3/chat/completions`,
      {
        model: VOLCANO_ARK_MODEL_NAME,
        messages: [
          { role: 'user', content: message },
        ],
        user: userId,
        // conversation_id: conversationId, // 火山方舟API可能不需要这个字段，或者有自己的会话管理方式
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${VOLCANO_ARK_API_KEY}`, // 使用单一API Key进行认证
        },
      }
  );

  // 根据火山方舟API的实际响应结构调整
  const botResponse = response.data.choices[0].message.content;
  console.log('Volcano Ark Response:', botResponse);
  return botResponse;
} catch (error) {
  console.error('Error sending message to Volcano Ark:', error);
  return 'Error communicating with Volcano Ark AI.';
}
}